package cc.mrbird.febs.system.manager;

import cc.mrbird.febs.common.service.CacheService;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.system.domain.Area;
import cc.mrbird.febs.system.domain.THouse;
import cc.mrbird.febs.system.domain.TResidentHouse;
import cc.mrbird.febs.system.domain.User;
import cc.mrbird.febs.system.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 封装一些和 User相关的业务操作
 */
@Service
public class TempInfoManager {

    @Resource
    private CacheService cacheService;
    @Resource
    private UserService userService;
    @Resource
    private AreaService areaService;
    @Resource
    private ITShopService itShopService;
    @Resource
    private ITHouseService itHouseService;
    @Resource
    private ITResidentHouseService itResidentHouseService;

    /**
     * 查找所有区域信息
     *
     * @return 区域信息
     */
    public List<Area> getAllArea() {
        return FebsUtil.selectCacheByTemplate(
                () -> this.cacheService.getArea(),
                () -> this.areaService.findAllAreaList());
    }

    public void loadAllAreaRedisCache(List<Area> Areas) throws Exception {
        cacheService.saveAllAreas(Areas);
    }

    /**
     * 查找所有商铺名称信息
     *
     * @return 区域信息
     */
    public List<String> getAllShopName() {
        return FebsUtil.selectCacheByTemplate(
                () -> this.cacheService.getAllShopName(),
                () -> this.itShopService.findAllShopNames());
    }

    /**
     * 将用户相关信息添加到 Redis缓存中
     *
     */
    public void loadAllShopNameRedisCache(List<String> shopNames) throws Exception {
        cacheService.saveAllShopNames(shopNames);
    }

    /**
     * 通过用户 id集合批量删除用户 Redis缓存
     *
     * @param userIds userIds
     */
    public void deleteUserRedisCache(String... userIds) throws Exception {
        for (String userId : userIds) {
            User user = userService.getById(userId);
            if (user != null) {
                cacheService.deleteUser(user.getUsername());
                cacheService.deleteRoles(user.getUsername());
                cacheService.deletePermissions(user.getUsername());
                cacheService.deleteUserSubordinates(user.getDeptId());
            }
            cacheService.deleteUserConfigs(userId);
        }
    }

    public List<String> getAllShopCode() {
        return FebsUtil.selectCacheByTemplate(
                () -> this.cacheService.getAllShopCode(),
                () -> this.itShopService.findAllShopCodes());
    }

    public void loadAllShopCodeRedisCache(List<String> shopCodes) throws Exception {
        cacheService.saveAllShopCodes(shopCodes);
    }

    public List<THouse> getAllHouseCode() {
        return FebsUtil.selectCacheByTemplate(
                () -> this.cacheService.getAllHouseCode(),
                () -> this.itHouseService.findAllHouseCodes());
    }

    public void loadAllHouseCodeRedisCache(List<THouse> houseCodes) throws Exception {
        cacheService.saveAllHouseCodes(houseCodes);
    }


    public List<TResidentHouse> getAllHouseOwner() {
        return FebsUtil.selectCacheByTemplate(
                () -> this.cacheService.getAllHouseOwner(),
                () -> this.itResidentHouseService.findAllHouseOwner());
    }

    public void loadAllHouseOwnerRedisCache(List<TResidentHouse> houseCodes) throws Exception {
        cacheService.saveAllHouseOwner(houseCodes);
    }
}
